package com.system.controller;

import com.system.pojo.Client;
import com.system.service.ClientService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@RequestMapping("/client")
public class ClientController {

	@Autowired
	private ClientService clientService;

	/**
	 * 添加新用户，必须添加默认收获地址
	 * @param client
	 * @return
	 */
	@RequestMapping(value = "/insert",method = RequestMethod.POST)
	public ResultVo<Client> insert(Client client) {
		ResultVo<Client> insert = new ResultVo<Client>();
		try {
			insert = clientService.insert(client);
		}catch (Exception e){
			e.printStackTrace();
			insert.setMessage("客户添加失败");
		}finally {
			return insert;
		}

	}


	/**
	 * 更新客户信息
	 * @param client
	 * @return
	 */
	@RequestMapping("/update")
	public ResultVo<Client> update(Client client) {
		ResultVo<Client> update = new ResultVo<>();
		try {
			update = clientService.updateById(client);
		}catch (Exception e){
			e.printStackTrace();
			update.setMessage("客户信息更新失败");
		}finally {
			return update;
		}
	}

	@RequestMapping("/defaultAdd/clientId/{clientId}/addressId/{addressId}")
	public ResultVo<Client> setDefaultAddress(@PathVariable Integer clientId,@PathVariable Integer addressId) {
		ResultVo<Client> update = new ResultVo<>();
		try {
			Client client = new Client();
			client.setId(clientId);
			client.setAddressId(addressId);
			update = clientService.updateById(client);
		}catch (Exception e){
			e.printStackTrace();
			update.setMessage("信息更新失败");
		}finally {
			return update;
		}
	}
	

	@RequestMapping("/selectAll")
	public ResultVo<Client> selectAllWithDefaultAddress() {
		ResultVo<Client> clientVo = new ResultVo<>();
		try {
			clientVo = clientService.selectAll();
		}catch (Exception e){
			e.printStackTrace();
			clientVo.setMessage("查询客户信息失败");
		}finally {
			return clientVo;
		}
	}


	@RequestMapping("/detail/{clientId}")
	public ResultVo<Client> detail (@PathVariable Integer clientId){
		ResultVo<Client> resultVo = new ResultVo<Client>();
		try {
			resultVo = clientService.detail(clientId);
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("查询失败");
		}finally {
			return resultVo;
		}
	}
}
